package org.dp.leeCode0053;

public class Solution {

    public static void main(String[] args) {

        Solution solution=new Solution();
        int[] nums = {-2,1};
        System.out.println(solution.maxSubArray2(nums));

    }

    public int maxSubArray(int[] nums) {
        int max=nums[0],temp;
        for (int i=0;i<nums.length;i++){
            temp=nums[i];
            if (max<temp){
                max=temp;
            }
            for (int j=i+1;j<nums.length;j++){
                temp+=nums[j];
                if (max<temp){
                    max=temp;
                }

            }
        }
        return max;

    }

    public int maxSubArray2(int[] nums) {
        int pre = 0, maxAns = nums[0];
        for (int x : nums) {
            pre = Math.max(pre + x, x);
            maxAns = Math.max(maxAns, pre);
        }
        return maxAns;
    }

}
